<template>
	<view class="d-login">

		<view class="d-login-btn">
			<template v-if="step == 1">
				<u-button class="d-btn" @click="pay()" type="primary" shape="circle" :custom-style="customStyle">确 定
				</u-button>
			</template>
		</view>
		<view class="d-login-btn1" style="">
			<template v-if="step == 1">

				<text style="background-color: rgba(0,0,0,0);color: white;">
					您还不是本项目付费会员，有些关键功能无法使用。
				</text>
				<u-form-item label="" prop="choice" label-width="2000rpx" required>
					<u-radio-group v-model="choice" wrap=true active-color='#2979ff'
						style="width:90%; flex;flex-direction: column;padding: 50rpx;flex: 2;">
						<u-radio name="1" style="margin-bottom: 20rpx;">
							<text style="color: white;font-size: 14px;">月度会员19.8元</text>
						</u-radio>
						<u-radio name="6" style="margin-bottom: 20rpx;">
							<text style="color: white;font-size: 14px;">半年会员68元</text>
							<text style="color: red">（推荐）</text>
						</u-radio>
						<u-radio name="12">

							<text style="color: white;font-size: 14px;">全年会员128元</text>
						</u-radio>
					</u-radio-group>
				</u-form-item>
			</template>

		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	const obj = {
		0: {
			lable: '工人',
			value: 'any'
		},
		1: {
			label: '普通小中介',
			value: 'small'
		},
		2: {
			label: '专属小中介',
			value: 'small'
		},
		3: {
			label: '大中介',
			value: 'big'
		}
	};
	export default {
		data() {
			return {
				customStyle: {
					background: '#5650ee'
				},
				step: 1,
				type: 1, //普通小中介
				choice: 6,
				id: '',
				params: {
					sex: '',
					openId: '',
					sessionKey: '',
					encryptedData: '',
					iv: '',
					hidPic: '',
					realName: '',
					recId: '',
					rId: '',
					code: '',
					type: 0
				}
			};
		},
		onLoad(e) {
			console.log(e)
			if (e && e.type) {
				this.params.type = e.type;
			}
			// this.form.rId = uni.getStorageSync('recId');
			this.recId = uni.getStorageSync('recId');
			var that = this;
			uni.login({
				provider: 'weixin',
				success: loginRes => {
					this.$u.api
						.postGetWechatOpenId({
							code: loginRes.code
						})
						.then(res => {
							console.log(res, '[登录页面-获取微信openid]');
							this.params.openId = res.data.openid;
							this.params.sessionKey = res.data.session_key;
							uni.setStorageSync('openId', this.params.openId);
							// if (!!res.data.phone) {
							// 	// 登录
							// 	this.postLoginCheck();
							// } else {
							// 	this.step = 1;
							// 	uni.showToast({
							// 		title: '请授权手机号',
							// 		icon: 'none'
							// 	})
							// }
						})
						.catch(err => {
							console.log(err);
						});
				}
			});
		},
		methods: {
			back() {
				uni.reLaunch({
					url: 'index/index'
				})
			},
			pay() {
				var that = this
				that.$u.api.postMyOrder({
					month: that.choice
				}).then(res => {
					if (res.status == true) {
						that.id = res.data.id
						uni.requestPayment({
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							package: res.data.package,
							signType: res.data.signType,
							paySign: res.data.paySign,
							success: e => {
								console.log(e, '支付回调')
								if (e.errMsg === 'requestPayment:ok') {
									let params = {
										id: that.id,
									}
									that.$u.api.postOrderSuccess(params).then(res2 => {
										console.log(res2)
										uni.showToast({
											title: '支付成功',
											duration: 2000,

										});
									})
								}
							}
						})

					}
				}).catch(err => {
					console.log(err);
				});
			},
			// 判断用户是否登录
			postIsLogin() {
				this.$u.api
					.postIsLogin()
					.then(res => {
						if (res.code == 1000) {
							console.log(res, '[登录页面-判断用户是否登录]');
							// 获取用户信息
							this.postGetAdminInfo();
							uni.reLaunch({
								url: './index/home'
							});
						}
					})
					.catch(err => {
						console.log(err);
					});
			},
			// 获取微信用户信息
			getuserinfo(e) {
				this.params.sex = e.detail.userInfo.gender;
				this.params.hidPic = e.detail.userInfo.avatarUrl;
				this.params.realName = e.detail.userInfo.nickName;
				// 获取微信openid
				this.postGetWechatOpenId();
			},
			// 获取微信openid
			postGetWechatOpenId() {
				uni.login({
					provider: 'weixin',
					success: loginRes => {
						this.$u.api
							.postGetWechatOpenId({
								code: loginRes.code
							})
							.then(res => {
								console.log(res, '[登录页面-获取微信openid]');
								this.params.openId = res.data.openid;
								this.params.sessionKey = res.data.session_key;
								uni.setStorageSync('openId', this.params.openId);
								if (!!res.data.phone) {
									// 登录
									this.postLoginCheck();
								} else {
									this.step = 2;
									uni.showToast({
										title: '请授权手机号',
										icon: 'none'
									});
								}
							})
							.catch(err => {
								console.log(err);
							});
					}
				});
			},
			// 授权手机号
			getphonenumber(e) {
				console.log(e.phoneNumber);
				if (e.detail.errMsg == 'getPhoneNumber:ok') {
					this.params.encryptedData = e.detail.encryptedData;
					this.params.iv = e.detail.iv;
					this.params.recId = uni.getStorageSync('recId');
					this.params.rId = uni.getStorageSync('recId');
					// 登录
					this.postLoginCheck();
				} else {
					uni.alert('若拒绝无法体验更多功能');
				}
			},
			// 登录
			postLoginCheck() {
				let params = {
					...this.params
				};
				this.$u.api
					.postLoginCheck(params)
					.then(res => {
						if (res.status == true) {
							console.log(res, '[登录页面-登录]');
							uni.setStorageSync('token', res.data);
							// 获取用户信息
							this.postGetAdminInfo().then(res => {
								// uni.navigateBack()
								uni.reLaunch({
									url: './index/home'
								});
							});
						}
					})
					.catch(err => {
						console.log(err);
					});
			},
			// 获取用户信息
			postGetAdminInfo() {
				return new Promise((resolve, reject) => {
					this.$u.api
						.postGetAdminInfo()
						.then(res => {
							console.log(res,'+++++++++++')
							if (res.status == true) {
								uni.setStorageSync('userPhone', res.data.phone);
								// this.$store.commit('commitUserId', res.data.id);
								uni.setStorageSync('userId', res.data.id);
								uni.setStorageSync('userInfo', res.data);
								this.$store.commit('commitUserInfo', res.data);
								switch (res.data.type) {
									case 0:
										uni.setStorageSync('role', 'any');
										uni.setStorageSync('roleType', 0);
										break;
									case 1:
										uni.setStorageSync('role', 'small');
										uni.setStorageSync('roleType', 1);
										break;
									case 2:
										uni.setStorageSync('role', 'small');
										uni.setStorageSync('roleType', 2);
									case 3:
										uni.setStorageSync('role', 'big');
										uni.setStorageSync('roleType', 3);
								}
							}
							resolve();
						})
						.catch(err => {
							console.log(err);
							reject();
						});
				});
			}
		}
	};
</script>

<style scoped lang="scss">
	.u-radio__label {
		// width: 100%!important;
	}

	.d-login {
		position: fixed;
		top: 0;
		width: 100%;
		height: 100%;
		background: url() no-repeat left top;
		background-size: 100% auto;
		background-color: #fff;

		.d-login-btn {
			position: fixed;
			bottom: 170rpx;
			left: 0;
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center;

			.d-btn {
				width: 690rpx;
			}
		}

		.d-login-btn1 {
			position: relative;
			overflow: hidden;
			-webkit-line-clamp: 2;
			text-overflow: ellipsis;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			top: 260rpx;

			width: 90%;
			background-color: rgba(0, 0, 0, 0.3);
			color: #333333;
			font-weight: 600;
			margin-left: auto;
			margin-right: auto;

			border-radius: 20px;
			padding: 20px;

			.d-btn {
				width: 690rpx;
			}
		}

		.bg {}
	}
</style>
